package com.fantaike.algorithm.test;

import com.fantaike.algorithm.sort.Insertion;
import com.fantaike.algorithm.sort.Merge;
import com.fantaike.algorithm.sort.Shell;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

/**
 * 测试几个排序的执行时间
 * @author jishushen
 * @create 2021-08-08 12:32
 */
public class SortCompare {
    /** 创建一个10000个数子倒叙排序的文件  **/
    public static void file() throws IOException {
        String path = "src/main/java/com/fantaike/algorithm/reverse_merge_shell.txt";
        File file = new File(path);
        if (!file.exists())
            file.createNewFile();

        BufferedWriter bw = new BufferedWriter(new FileWriter(file));
        for (int i = 100000;i>0;i--){
            bw.write(String.valueOf(i));
            bw.newLine();
        }
        bw.flush();
        bw.close();
    }

    public static void main(String[] args) throws IOException {
        //获取rever_merge_shell.txt里面的数据
        Integer[] array = readFile();
        //testShell(array); //20 毫秒
        //testMerge(array);  //50毫秒
        testInsertion(array);
    }

    /**读取 rever_merge_shell.txt 里面的文件**/
    public static Integer[] readFile() throws IOException {
        List<Integer> list = new ArrayList<>();
        BufferedReader br = new BufferedReader(new FileReader("src/main/java/com/fantaike/algorithm/reverse_merge_shell.txt"));
        String line = null;
        while ((line = br.readLine()) != null){
            list.add(Integer.valueOf(line));
        }
        br.close();
        //把集合转换成数组
        Integer[] array = list.toArray(new Integer[list.size()]);
        return array;
    }

    /** 希尔排序 **/
    public static void testShell(Integer[] array){
        long startTime = System.currentTimeMillis();
        Shell.sort(array);
        long endTime = System.currentTimeMillis();
        System.out.println("希尔排序执行时间: " + (endTime - startTime) + " 毫秒" );
    }

    /** 归并排序**/
    public static void testMerge(Integer[] array){
        long startTime = System.currentTimeMillis();
        Merge.sort(array);
        long endTime = System.currentTimeMillis();
        System.out.println("希尔排序执行时间: " + (endTime - startTime) + " 毫秒" );
    }

    /* 测试插入排序 */
    public static void testInsertion(Integer[] array){
        long startTime = System.currentTimeMillis();
        Insertion.sort(array);
        long endTime = System.currentTimeMillis();
        System.out.println("希尔排序执行时间: " + (endTime - startTime) + " 毫秒" );
    }
}
